import pymysql
import pandas as pd
import requests,json
import datetime


def read_sqldata(sql_table,sql_table_columns,start_time,end_time,con):
    '''
    Function: 生成SQL命令,区分中兴设备和华为设备（中兴和华为设备的字段不一致）
    :param sql_table: str, SQL中的表名
    :param sql_table_columns: str,SQL表中的字段名称
    :param start_time: str, 查询时间，即SQL表中的‘时间’对应的数值
     示例：start_time="('2022-01-19 10:00:00','2022-01-20 10:00:00')"
    :return sql_command, str, SQL命令
    '''
    # sql_table_columns=','.join(x for x in sql_table_columns)

    sql_command = "SELECT `时间`,`对象名称`,`"   + sql_table_columns + "` FROM " + sql_table + " WHERE 时间 BETWEEN '" + start_time +"' AND '"+ end_time  + "' ORDER BY 时间;"

    data_sql = pd.read_sql(sql_command, con)  # 读取SQL数据
    return data_sql


#调用测试--数据json处理
start_time = (datetime.datetime.now() - datetime.timedelta(days=365)).strftime("%Y-%m-%d %H:00:00")
end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:00:00")

start_time='2023-01-01 00:00:00'
# end_time='2022-08-24 00:00:00'

con = pymysql.connect(host="xxx", user="xxx", passwd="xx#xx", db="xx", charset="utf8")
print(start_time,end_time)

zhenj_4G_usernum_HW = read_sqldata("5gc_4gamftacuser_zhj_q", "117494568", start_time, end_time, con)






